package com.guolibbs.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.guolibbs.vo.Reply;
import com.guolibbs.vo.Topic;

/**
 * Servlet implementation class Wltopic
 */
public class Wltopic extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Wltopic() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	@SuppressWarnings("unchecked")
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String topicName = request.getParameter("tname");
		if(topicName==null||"".equals(topicName)){
			topicName = (String) request.getSession().getAttribute("tname");
		}
		System.out.println("topicName="+topicName);
		List<Topic> topicList = (List<Topic>) this.getServletContext().getAttribute("topics");
		if(topicList!=null){
			Topic topic = null;
			for(Topic t:topicList){
				if(topicName.equals(t.getTopicName())){
					topic = t;
				}
			}
			List<Reply> replyListAll = (List<Reply>) this.getServletContext().getAttribute("replysall");
			List<Reply> replyList = new ArrayList<Reply>();
			if(replyListAll==null){
				replyListAll = new ArrayList<Reply>();
				this.getServletContext().setAttribute("replysall",replyListAll);
			}
			for(Reply r : replyListAll){
				if(r.getTopicNo().equals(topic.getTopicName())){
					replyList.add(r);
				}
			}
			request.setAttribute("topic", topic);
			request.setAttribute("replyList", replyList);
			request.getRequestDispatcher("WEB-INF/jsp/wltopic.jsp").forward(request, response);
		}else{
			request.setAttribute("err", "请登录...");
			request.getRequestDispatcher("WEB-INF/jsp/login.jsp").forward(request, response);
		}
	}

}
